/*
 * Copyright 2009 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package util{
	import org.splink.resource.resourcebundles.resourcebundle.fonts.FontData;

	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * A static factory class which enables the creation of different
	 * kinds of <code>TextField</code>s conveniently. This includes 
	 * single and multiline dynamic and input TextFields.
	 * 
	 * @author Max Kugland
	 */
	public class TextFieldFactory {

		/**
		 * Creates a single line TextField. The TextField automatically fits
		 * the given text.
		 * 
		 * @param text the text which is displayed in the <code>TextField</code>
		 * @param fontSize the font size of the <code>TextField</code>
		 * @param data a <code>FontData</code> object which describes the font
		 * used to render the text
		 * 
		 * @return a single line <code>TextField</code> which displays the given text in the given
		 * size using the given <code>FontData</code>
		 * 
		 * @see flash.text.TextField
		 * @see org.splink.resource.resourcebundles.resourcebundle.fonts.FontData
		 */
		public static function singleline(text : String, fontSize : int, data : FontData = null) : TextField {
			data = checkFontData(data);
			var tf : TextField = new TextField();
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.mouseWheelEnabled = tf.selectable = false;
			tf.defaultTextFormat = createTextformat(fontSize, data);
			tf.multiline = tf.wordWrap = false;
			tf.htmlText = text;
			applyFontData(data, tf);
			return tf;
		}
		
		private static function checkFontData(data : FontData) : FontData {
			if(data) return data;
			data = new FontData();
			data.isSystemFont = true;
			data.id = "Arial";
			return data;
		}

		public static function multiline(text : String, fontSize : int, data : FontData, width : int) : TextField {
			data = checkFontData(data);
			var tf : TextField = new TextField();
			tf.mouseWheelEnabled = tf.selectable = false;
			tf.defaultTextFormat = createTextformat(fontSize, data);
			
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.htmlText = text;
			tf.wordWrap = tf.multiline = true;
			tf.width = width;
			
			applyFontData(data, tf);
			
			return tf;
		}
		
		private static function applyFontData(data : FontData, tf : TextField) : void {
			tf.embedFonts = !data.isSystemFont;
			if(!isNaN(data.xOffset)) tf.x += data.xOffset;
			if(!isNaN(data.yOffset)) tf.y += data.yOffset;
		}

		public static function createTextformat(size : int, data : FontData) : TextFormat {
			var format : TextFormat = new TextFormat();
			format.size = size;
			if(!isNaN(data.sizeOffset)) {
				format.size += data.sizeOffset;
			}

			format.font = data.id;
			format.color = 0;
			format.leading = 2;
			return format;
		}	
	}
}
